/*******************************************************************************
 * Copyright (c) 2014-9-3 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.foreveross.project.projectonline.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-9-3
 */
public class GroupConcatSplitUtil {

	public static Map<String, String> splitToMap(String content) {
		return splitToMap(content, "@@,,@@", "##,,##");
	}

	public static Map<String, String> splitToMap(String content, String groupSplit, String concatSplit) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		if (StringUtils.isBlank(content)) {
			return map;
		}
		String[] split = content.split(groupSplit);
		for (String s : split) {
			String[] split2 = s.split(concatSplit);
			if (split2 != null && split2.length == 2) {
				map.put(split2[0], split2[1]);
			}
		}
		return map;
	}

	public static String[] splitToArray(String content) {
		return splitToArray(content, "@@,,@@");
	}

	public static String[] splitToArray(String content, String groupSplit) {
		if (StringUtils.isBlank(content)) {
			return null;
		}
		return content.split(groupSplit);
	}

	public static Map<String, List<String>> splitToMapList(String content) {
		return splitToMapList(content, "@@,,@@", "##,,##");
	}

	public static Map<String, List<String>> splitToMapList(String content, String groupSplit, String concatSplit) {
		Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
		if (StringUtils.isBlank(content)) {
			return map;
		}
		String[] split = content.split(groupSplit);
		for (String s : split) {
			String[] split2 = s.split(concatSplit);
			if (split2 != null && split2.length > 1) {
				List<String> list = new ArrayList<String>();
				for (int i = 1; i < split2.length; i++) {
					list.add(split2[i]);
				}
				map.put(split2[0], list);
			}
		}
		return map;
	}
}
